package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsCommentLike;
import com.uyayo.cms.mapper.CmsCommentLikeMapper;
import com.uyayo.cms.model.CmsCommentLikeCondition;
import com.uyayo.cms.service.CmsCommentLikeService;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsCommentLikeService")
public class CmsCommentLikeServiceImpl extends BaseServiceImpl<CmsCommentLike> implements CmsCommentLikeService {

	private final CmsCommentLikeMapper cmsCommentLikeMapper;

	public CmsCommentLikeServiceImpl(CmsCommentLikeMapper cmsCommentLikeMapper) {
		this.cmsCommentLikeMapper = cmsCommentLikeMapper;
	}

	public List<CmsCommentLike> selectCmsCommentLike(CmsCommentLikeCondition cmsCommentLikeCondition) {
		Example example = new Example(CmsCommentLike.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(cmsCommentLikeCondition);
		example.createCriteria();
		return cmsCommentLikeMapper.selectByExample(example);
	}

	@Override
	public PageBean<CmsCommentLike> selectCommentLikeByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsCommentLikeCondition cmsCommentLikeCondition) {

		List<CmsCommentLike> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);

		Example example = new Example(CmsCommentLike.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Example.Criteria criteria = example.createCriteria();
		if(cmsCommentLikeCondition.getBeginCreateTime()!=null || cmsCommentLikeCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsCommentLikeCondition.getBeginCreateTime(), cmsCommentLikeCondition.getEndCreateTime());
		}
		if(Objects.nonNull(cmsCommentLikeCondition.getStatus())){
			criteria.andEqualTo("status",cmsCommentLikeCondition.getStatus());
		}
		if(Objects.nonNull(cmsCommentLikeCondition.getCommentId())){
			criteria.andEqualTo("CommentId",cmsCommentLikeCondition.getCommentId());
		}
		if(Objects.nonNull(cmsCommentLikeCondition.getMemberId())){
			criteria.andEqualTo("memberId",cmsCommentLikeCondition.getMemberId());
		}
		if(Objects.nonNull(cmsCommentLikeCondition.getLikeType())){
			criteria.andEqualTo("likeType",cmsCommentLikeCondition.getLikeType());
		}
		userList = cmsCommentLikeMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsCommentLike> pageInfo = new PageInfo<>(userList);

		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
}
